package com.simplegui.api;

import com.simplegui.api.handlers.DrawHandler;
import com.simplegui.api.handlers.KeyDownHandler;
import com.simplegui.api.handlers.KeyUpHandler;

public abstract class DynamicFrame {

	private FrameRateThread frameRateThread;
	
	public DynamicFrame() {
		frameRateThread = new FrameRateThread(this);
	}
	
	public void draw() {
		Canvas canvas = getCanvas();
		if (getDrawHandler() != null && canvas != null){
			canvas.clear();
			getDrawHandler().draw(canvas);
			repaint();
		}
	}
	
	public void start(){
		if (!frameRateThread.isRunning()){
			new Thread(frameRateThread).start();
		}
	}
	
	public FrameRateThread getFrameRateThread() {
		return frameRateThread;
	}
	
	protected abstract Canvas getCanvas();
	
	protected abstract void repaint();
	
	protected abstract DrawHandler getDrawHandler();
	
	protected abstract KeyUpHandler getKeyUpHandler();
	
	protected abstract KeyDownHandler getKeyDownHandler();

}
